
const request = require('../../../../../utils/request');

Page({
    data: {
        attr: ''
    },
    onReady: function() {
        wx.setNavigationBarTitle({
            title: '修改地址'
        })
    },
    onLoad: function(e) {
        let attr = JSON.parse(e.attr)
        this.setData({
            attr: attr
        })
    },
    goLocation() {
        var that = this
        wx.chooseLocation({
            success: function(e){
                console.log(e)
                that.setData({
                    'attr.address': e.address
                })
            },
            fail: function(e){
                console.log("选择地址失败")
            }
        })
    },

    //修改地址
    reviseAttr(e){
        console.log(e)
        var fromData = e.detail.value
        var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
        if(!myreg.test(fromData.mobile)){
          wx.showModal({
              title: '提示',
              content: '请输入正确的手机号码',
              showCancel: false,
          })
        }else{
          request.HttpRequst(true, 'user/addAddress', false, '',{ "id":this.data.attr.id, "address":this.data.attr.address,"mobile": fromData.mobile,"address_detail": fromData.address_detail,"name": fromData.name,"token": wx.getStorageSync("sessionId")}, "post", false, res =>{
              console.log(res)
              if (res.code==200){
                wx.navigateBack({     //返回上一页面或多级页面
                  delta: 1
                })
              }else{
                wx.showModal({
                    title: '提示',
                    content: res.info,
                    showCancel: false
                })
              }
          })
        }   
    },

    //删除地址
    deleteAdd(e) {
        console.log(e.target.dataset.id)
        var that = this
        wx.showModal({
          title: '提示',
          content: '确定要删除这收货地址？',
          success: function (res) {
            if (res.confirm) {
              //请求接口删除数据
              request.HttpRequst(true, 'user/deleteAddress', false, '', { "id": e.target.dataset.id,"token": wx.getStorageSync("sessionId") }, "post", false,
              res=>{
                if(res.code == 200){
                    wx.navigateBack({     //返回上一页面或多级页面
                        delta: 1
                    })
                }else{
                  wx.showModal({
                      title: '提示',
                      content: '删除失败!',
                      showCancel: false,
                  })
                }
              })
            }else{
              console.log('用户点击取消')
            }
          }
        })
    }
})